#include "sqlite.hpp"
#include <iostream>
#include <cassert>

// 查到一行，回调一次
int seletcStudentCallback(void* arg, int colCount, char** result, char** fieldName) 
{
    std::vector<std::string>* pvec = static_cast<std::vector<std::string>*>(arg);
    std::string col = "|";
    for(int i = 0; i < colCount; i++) 
    {
        col += fieldName[i];
        col += ": ";
        col += result[i];
        col += "|";
    }
    pvec->push_back(col);
    return 0; //成功返回0，否则进程Abort
}

int main()
{
    SqliteHelper helper("./test.db");
    assert(helper.open()); //打开库文件（建库，sqlite一库一文件，不用create database）

    const char* creatTableSql = "create table if not exists student(id int primary key, name varchar(32), age int);";
    assert(helper.execute(creatTableSql, nullptr, nullptr));

    // const char* insertSql = "insert into student values(1, 'Texas', 19), (2, 'Lapland', 19), (3, 'skadi', 19);";
    // assert(helper.execute(insertSql, nullptr, nullptr));

    // const char* updateSql = "update student set name='amiya' where id = 3;";
    // assert(helper.execute(updateSql, nullptr, nullptr));

    // const char* deleteSql = "delete from student where id=3;";
    // assert(helper.execute(deleteSql, nullptr, nullptr));
        
    std::vector<std::string> vec;
    const char* selectSql = "select * from student;";
    assert(helper.execute(selectSql, seletcStudentCallback, &vec));

    for(auto& name : vec)
    {
        std::cout << name << std::endl;
    }

    helper.close();

    return 0;
}